// components/statistics/matchholestatistics/matchholestatistics.js
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    matchinfo: {
      type: Object,
      value: undefined,
      observer: function (newVal, oldVal) {

       // this._matchinfopropertyChange(newVal, oldVal);

      } // 属性被改变时执行的函数（可选），也可以写成在methods段中定义的方法名字符串, 如：'_propertyChange'

    },
    holestatisc: {
      type: Object,
      value: undefined,
      observer: function (newVal, oldVal) {
       // this._matchleaderspropertyChange(newVal, oldVal);
      } // 属性被改变时执行的函数（可选），也可以写成在methods段中定义的方法名字符串, 如：'_propertyChange'

    },

    scoresstatisc:{
      type: Object,
      value: undefined,
      observer: function (newVal, oldVal) {
       // this._matchleaderspropertyChange(newVal, oldVal);
      } // 属性被改变时执行的函数（可选），也可以写成在methods段中定义的方法名字符串, 如：'_propertyChange'
    },

    sstatisc:{
      type: Object,
      value: undefined,
      observer: function (newVal, oldVal) {
       // this._matchleaderspropertyChange(newVal, oldVal);
      } // 属性被改变时执行的函数（可选），也可以写成在methods段中定义的方法名字符串, 如：'_propertyChange'
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    staticUrl: app.globalData.staticUrl,
    selectholestatictroundindex: 0,
    rankindex: 0,
    ranktype: 0,
    currentholestaticlistsData:0,
    display:0,
    scoresstatiscindex:0,
    sstatiscindex:0,
    sstatisc:'',
  },

  /**
   * 组件的方法列表
   */
  methods: {
    //改变球洞统计组别
    checkholestaticlistsData: function (e) {
      var current = e.currentTarget.dataset.current;
      this.setData({
        currentholestaticlistsData: current,
        rankindex: 0,
        ranktype: 0,
      });
    },


    changestatisc:function(e){
      var value = e.detail.value;
      
      this.setData({
        sstatiscindex: value,
      
      });
    },

    changescorestatisc:function(e){
      var value = e.detail.value;
      
      this.setData({
        scoresstatiscindex: value,
      
      });
    },

    //改变球洞统计轮次
    changeholestatiscround(e) {
      var value = e.detail.value;
      console.log(this.data.matchinfo.rounds[value].roundno);
      this.setData({
        selectholestatictroundindex: value,
        rankindex: 0,
        ranktype: 0,
      });

      //console.log(this.data.matchstatisc.list[this.data.statiscindex].statiscname);
      this.triggerEvent('getholestatiscs', this.data.matchinfo.rounds[value].roundno);
      //this.getholestatiscs( this.data.matchinfo.rounds[value].roundno);
    },


    changeholestatiscroundranktype: function (e) {
      var rankindex = e.currentTarget.dataset.rankindex;
      var ranktype = this.data.ranktype;
      var holestatiscgroup = this.data.holestatisc;
      var holestatisc = holestatiscgroup.lists[this.data.currentholestaticlistsData].holestatisc;
      console.log(holestatisc);
      if (!holestatisc && !holestatisc.length <= 0) {
        return;
      }
      if (this.data.rankindex != rankindex) {
        this.setData({
          ranktype: 0,
          rankindex: rankindex,
        });
      } else {
        this.setData({
          ranktype: !ranktype,
          rankindex: rankindex,
        });
      }
      var property = "";
      if (rankindex == 0) {
        property = "pos";
      }
      if (rankindex == 1) {
        property = "sc_ho_id";
      }
      if (rankindex == 2) {
        property = "sc_par";
      }
      if (rankindex == 3) {
        property = "agv";
      }
      if (rankindex == 4) {
        property = "eagles";
      }
      if (rankindex == 5) {
        property = "birdie";
      }
      if (rankindex == 6) {
        property = "par";
      }
      if (rankindex == 7) {
        property = "bogey";
      }
      if (rankindex == 8) {
        property = "dbogey";
      }
      if (rankindex == 9) {
        property = "worst";
      }
      holestatisc.sort(this.compare(property));
      console.log(holestatisc);
      holestatiscgroup.lists[this.data.currentholestaticlistsData].holestatisc = holestatisc;
      this.setData({
        holestatisc: holestatiscgroup
      });
    },
    compare: function (property) {
      var that = this;
      return function (a, b) {
        var value1 = a[property];
        var value2 = b[property];
        if (that.data.ranktype == 1) {
          return value2 - value1;
        } else {
          return value1 - value2;
        }
      }
    },

    
   ///赛事detail
   swichNavdisplay(e) {
    var selected = e.currentTarget.dataset.current;
   
    this.setData({
      display: selected
    })
  },


  //打开playerscorecard 页面 
  _openplayerCard(e) {
    this.setData({
      selectplayerid: e.currentTarget.dataset.id,
      teamorplayer: 0
    });
    var playername = e.currentTarget.dataset.playername;
    let id = (this.data.teamorplayer == 0) ? this.data.selectplayerid : this.data.selectteamid;
    // 微信小程序中是通过triggerEvent来给父组件传递信息的
    this.triggerEvent('_openplayerCard', {
      playerorteam: this.data.teamorplayer,
      selectid: id,
      playername: playername,
      matchinfo: this.data.matchinfo
    }) // 将参数的形式传递给父组件

  },

  }
})